package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.InterviewCollection;
import com.example.demo.mapper.InterviewCollectionMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 ** Create by Wedream on 2021/7/16
 * 面试集合
 */
@RestController
@RequestMapping("/interviewCollection")
public class InterviewCollectionController {
    
    @Resource
    InterviewCollectionMapper interviewCollectionMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody InterviewCollection interviewCollection) {
        interviewCollectionMapper.insert(interviewCollection);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        interviewCollectionMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String type) {

        LambdaQueryWrapper<InterviewCollection> wrapper = Wrappers.<InterviewCollection>lambdaQuery();
        if(StrUtil.isNotBlank(type)) {
           wrapper.like(InterviewCollection::getType,type);
        }
        Page<InterviewCollection> InterviewCollectionPage = interviewCollectionMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(InterviewCollectionPage);
    }

    @PutMapping //改
    public Result<?> update(@RequestBody InterviewCollection interviewCollection) {
        interviewCollectionMapper.updateById(interviewCollection);
        return Result.success();
    }
}
